package org.jeecg.modules.departs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.departs.entity.DepartDevicesEntity;
import org.jeecg.modules.departs.entity.TigerDepartDevice;
import org.jeecg.modules.departs.mapper.TigerDepartDeviceMapper;
import org.jeecg.modules.departs.service.ITigerDepartDeviceService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @Description: 机构设备
 * @Author: jeecg-boot
 * @Date:   2022-07-22
 * @Version: V1.0
 */
@Service
public class TigerDepartDeviceServiceImpl extends ServiceImpl<TigerDepartDeviceMapper, TigerDepartDevice> implements ITigerDepartDeviceService {

    @Override
    public boolean saveBatch(DepartDevicesEntity departDevicesEntity) {
        try{
            //删除旧记录
            QueryWrapper<TigerDepartDevice> queryMapper = new QueryWrapper<>();
            queryMapper.eq("depart_id", departDevicesEntity.getDepartId());
            this.getBaseMapper().delete(queryMapper);
            //增加新记录
            List<TigerDepartDevice> data = new ArrayList<>();
            for(int i=0;i<departDevicesEntity.getDevices().size();i++){
               TigerDepartDevice t = new TigerDepartDevice();
               t.setDelFlag("0");
               t.setDepartId(departDevicesEntity.getDepartId());
               t.setDeviceId(departDevicesEntity.getDevices().get(i));
               data.add(t);
            };
            this.saveBatch(data);

            return true;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }

    }
}
